package cn.iutils.attach.controller;

import cn.iutils.common.ResultVo;
import cn.iutils.common.config.JConfig;
import cn.iutils.common.utils.JFileUtils;
import cn.iutils.common.utils.JUploadUtils;
import com.google.common.collect.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.iutils.common.Page;
import cn.iutils.common.utils.JStringUtils;
import cn.iutils.common.controller.BaseController;
import cn.iutils.attach.entity.Attach;
import cn.iutils.attach.service.AttachService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
* 附件表 控制器
* @author iutils.cn
* @version 1.0
*/
@Controller
@RequestMapping("${adminPath}/attach")
public class AttachController extends BaseController {

    @Autowired
    private AttachService attachService;

    @ModelAttribute
    public Attach get(@RequestParam(required = false) String id) {
        Attach entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = attachService.get(id);
        }
        if (entity == null) {
            entity = new Attach();
        }
        return entity;
    }

    @RequiresPermissions("attach:view")
    @RequestMapping()
    public String list(Model model, Page<Attach> page) {
        page.setOrderBy("a.create_date desc");
        model.addAttribute("page", page.setList(attachService.findPage(page)));
        return "attach/list";
    }

    @RequiresPermissions("attach:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable("id") String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) {
        attachService.delete(id);
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/attach?pageNo="+pageNo+"&pageSize="+pageSize;
    }

    /**
     * 上传文件操作
     * 支持多附件上传
     * 事物控制
     */
    @Transactional
    @RequiresPermissions("attach:fileUpload")
    @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
    public String fileUpload(Attach attach,@RequestParam(value = "file", required = false) MultipartFile[] file,HttpServletRequest request, HttpServletResponse response){
        ResultVo resultVo = null;
        List<Attach> attachs = Lists.newArrayList();
        try {
            for(MultipartFile myfile : file){
                if(myfile.isEmpty()){
                    resultVo = new ResultVo(ResultVo.SUCCESS, "2", "请选择文件后上传", null);
                }else{
                    String originalFilename = myfile.getOriginalFilename();
                    File targetFile = JUploadUtils.save(myfile, request);
                    Attach item = new Attach();
                    item.setChannel(attach.getChannel());
                    item.setContentId(attach.getContentId());
                    item.setDownload_size(myfile.getSize());
                    item.setFileName(originalFilename);
                    item.setFileType(myfile.getContentType());
                    item.setFilePath(JConfig.getConfig(JConfig.FILEUPLOAD) + "/" + targetFile.getName());
                    attachService.save(item);
                    attachs.add(item);
                }
            }
            resultVo = new ResultVo(ResultVo.SUCCESS, "1", "上传成功", attachs);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo = new ResultVo(ResultVo.FAILURE, "-1", "上传失败", null);
        }
        return renderString(response, resultVo);
    }
}
